package quq.missq.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import quq.missq.R;
import quq.missq.adapter.BigShootFragmentAdapter;

/**
 * Created by River on 7/24/15.
 */
public class LoveBigShootFragment extends Fragment implements View.OnClickListener{

    private RelativeLayout mFragmentLayout;
    private ViewPager mViewPager;
    private BigShootFragmentAdapter adapter;

    // tabhost
    private TextView mAll;
    private TextView mPlan36;
    private TextView mChange72;
    private TextView mNight1001;
    private ArrayList<TextView> tabTexts;

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
        super.onCreateView(inflater, container, savedInstanceState);
        mFragmentLayout = (RelativeLayout)inflater.inflate(R.layout.love_big_shoot_fragment, container, false);
        initView();
        initData();
        initListener();
        return mFragmentLayout;
    }

    private void initView(){
        mViewPager = (ViewPager)mFragmentLayout.findViewById(R.id.viewPager);
        mViewPager.setOffscreenPageLimit(4);
        mAll = (TextView)mFragmentLayout.findViewById(R.id.allBigShoot);
        mPlan36 = (TextView)mFragmentLayout.findViewById(R.id.plan36);
        mChange72 = (TextView)mFragmentLayout.findViewById(R.id.change72);
        mNight1001 = (TextView)mFragmentLayout.findViewById(R.id.night1001);

    }

    @Override
    public void onResume() {
        super.onResume();

        if(adapter != null){
           // for (int i=0;i<adapter.getCount();i++){
//                Fragment fragment = adapter.getItem(0);
//                fragment.onResume();
//           // }
        }
    }

    private void initData(){
        tabTexts = new ArrayList<>();
        tabTexts.add(mAll);
        tabTexts.add(mPlan36);
        tabTexts.add(mChange72);
        tabTexts.add(mNight1001);
        mViewPager.setCurrentItem(0);
        changeTabTextStyle(0);
        adapter = new BigShootFragmentAdapter(this.getChildFragmentManager());
        mViewPager.setAdapter(adapter);
    }

    private void initListener(){
        mAll.setOnClickListener(this);
        mPlan36.setOnClickListener(this);
        mChange72.setOnClickListener(this);
        mNight1001.setOnClickListener(this);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPx) {
            }

            @Override
            public void onPageSelected(int i) {
                changeTabTextStyle(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {
            }
        });
    }

    private void changeTabTextStyle(int position){
        TextView curTv = tabTexts.get(position);
        curTv.setTextSize(15);
        curTv.setTextColor(getResources().getColor(R.color.xuanzhong_color));
        TextView otherTv;
        for(int i=0; i<tabTexts.size();i++){
            if(i != position){
                otherTv = tabTexts.get(i);
                otherTv.setTextSize(14);
                otherTv.setTextColor(getResources().getColor(android.R.color.darker_gray));
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.allBigShoot:
                mViewPager.setCurrentItem(0);
                changeTabTextStyle(0);
                break;
            case R.id.plan36:
                mViewPager.setCurrentItem(1);
                changeTabTextStyle(1);
                break;
            case R.id.change72:
                mViewPager.setCurrentItem(2);
                changeTabTextStyle(2);
                break;
            case R.id.night1001:
                mViewPager.setCurrentItem(3);
                changeTabTextStyle(3);
                break;
            default: break;
        }
    }
}
